﻿using System;
using System.Linq;
using StockholmsStad.Data.Context;
using StockholmsStad.Data.ContractFactory;
using StockholmsStad.Data.Interfaces;
using StockholmsStad.Runtime.Serialization.DataContract;
using StockholmsStad.Utilities.Enums;
using StockholmsStad.Utilities.Exceptions;

namespace StockholmsStad.Data.DatabaseAgents
{
    internal class PropertiesAgent : DBAgentBase, IPropertiesAgent
    {
        #region Implementation of IPropertiesAgent

        public ParVisProperties GetProperties(IDataContext context)
        {
            PropertiesFactory factory = FactoryAgent.GetPropertiesFactory();
            try
            {
                StockholmsStadDataContext dbCtx = context.GetInternalContext();
                Entity.ParVisProperties properties = dbCtx.ParVisProperties.OrderBy(p => p.PropertiesId).First();
                return factory.CreateContractObject(properties);
            }
            catch (Exception ex)
            {
                throw new StockholmStadSystemException(SystemErrorCode.DataAccessFailure, ex);
            }
        }

        public void SetProperties(IDataContext context, ParVisProperties properties)
        {
            try
            {
                StockholmsStadDataContext dbCtx = context.GetInternalContext();

                // Finns bara en rad i tabellen, eller första raden är den som gäller
                var entityProperties = dbCtx.ParVisProperties.Where(p => p.PropertiesId == 1).SingleOrDefault();

                if(entityProperties != null)
                {
                    entityProperties.ImpedimentDocValidMonths = properties.ImpedimentDocValidMonths;
                    entityProperties.IndividualImpedimentDocReminderAtDays = properties.IndividualImpedimentDocReminderAtDays;
                    entityProperties.IndividualImpedimentDocRemoveBookingAtDays = properties.IndividualImpedimentDocRemoveBookingAtDays;
                    entityProperties.IndividualPreliminaryBookingEarliestDays = properties.IndividualPreliminaryBookingEarliestDays;
                    entityProperties.ReminderOfDocumentsIndividualBookingDays = properties.ReminderOfDocumentsIndividualBookingDays;
                    entityProperties.ReminderOfServiceOrdinaryBookingDays = properties.ReminderOfServiceOrdinaryBookingDays;
                    entityProperties.OrdinaryImpedimentDocReminderAtDays = properties.OrdinaryImpedimentDocReminderAtDays;
                    entityProperties.OrdinaryImpedimentDocRemoveBookingAtDays = properties.OrdinaryImpedimentDocRemoveBookingAtDays;
                    entityProperties.OrdinaryPreliminaryBookingEarliestDays = properties.OrdinaryPreliminaryBookingEarliestDays;
                    entityProperties.ApplicationChangeDateIndividualBookingDays = properties.ApplicationChangeDateIndividualBookingDays;
                    entityProperties.ApplicationChangeDateOrdinaryBookingDays =
                        properties.ApplicationChangeDateOrdinaryBookingDays;

                    dbCtx.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                throw new StockholmStadSystemException(SystemErrorCode.DataAccessFailure, ex);
            }
            
        }

        #endregion
    }
}